package com.letoken.platform.repo.app.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.util.BeanUtil;
import com.letoken.platform.repo.admin.po.AdminUser;
import com.letoken.platform.repo.admin.redis.AdminUserInfoRedis;
import com.letoken.platform.repo.admin.res.user.AdminUserInfoRes;
import com.letoken.platform.repo.app.po.AppUser;
import com.letoken.platform.repo.utlis.WebUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;

/**
 * @version 1.0
 * @author： letokenlabs
 * @date： 2021/11/24 9:55
 * 用户登录凭证
 */
public class AppLoginTokenRedis {
    private StringRedisTemplate stringRedisTemplate;

    private AppLoginTokenRedis() {
        stringRedisTemplate = BeanUtil.getBean(StringRedisTemplate.class);
    }

    private enum Singleton {
        INSTANCE;
        private AppLoginTokenRedis instance;

        // JVM会保证此方法绝对只调用一次
        Singleton() {
            instance = new AppLoginTokenRedis();
        }
    }

    /**
     * 活动配置
     */
    String cacheKey = RedisKeyConstant.LETOKEN_APP_LOGIN;

    public static AppLoginTokenRedis me() {
        return AppLoginTokenRedis.Singleton.INSTANCE.instance;
    }

    public void put(String key, Object obj) {
        if (obj != null) {
            stringRedisTemplate.opsForHash().put(cacheKey, key, JSON.toJSONString(obj));
        }
    }

    public void putAll(Map<String, Object> map) {
        if (map != null) {
            stringRedisTemplate.opsForHash().putAll(cacheKey, map);
        }
    }

    public Object getValue(String key) {
        return stringRedisTemplate.opsForHash().get(cacheKey, key);
    }

    public String getStrValue(String key) {
        return (String) stringRedisTemplate.opsForHash().get(cacheKey, key);
    }

    public AppUser getClassObject(String key) {
        String valueStr = (String) stringRedisTemplate.opsForHash().get(cacheKey, key);
        if (valueStr == null) {
            return null;
        }
        AppUser classRedis = null;
        try {
            classRedis = JSONObject.toJavaObject(JSONObject.parseObject(valueStr), AppUser.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return classRedis;
    }

    public AdminUserInfoRes getAdminUserInfoRes() {
        String valueStr = AdminUserInfoRedis.me().getStrValue(WebUtils.getAuthorization());
        if (valueStr == null) {
            return null;
        }
        AdminUserInfoRes classRedis = null;
        try {
            classRedis = JSONObject.toJavaObject(JSONObject.parseObject(valueStr), AdminUserInfoRes.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return classRedis;
    }

    /**
     * @param key ... 多个key
     * @return
     */
    public Long del(String... key) {
        return stringRedisTemplate.opsForHash().delete(cacheKey, key);
    }
}
